<template>
  <div class="todo-list">
    <h2>自律一下</h2>
    <div class="add-todo">
      <input
        v-model="newTodoTitle"
        placeholder="输入新待办事项"
        @keyup.enter="addTodo"
      />
      <button @click="addTodo">添加</button>
    </div>
    <!-- 将待办事项列表移动到输入框下方 -->
    <div v-for="(todo, index) in todos" :key="index" class="todo-container">
      <TodoItem
        :title="todo.title"
        :completed="todo.completed"
        @update:completed="updateTodoCompleted(index, $event)"
        @remove-todo="removeTodo(index)"
      />
    </div>
  </div>
</template>

<script>
import TodoItem from './TodoItem.vue';

export default {
  components: {
    TodoItem,
  },
  data() {
    return {
      todos: [],
      newTodoTitle: '',
    };
  },
  methods: {
    addTodo() {
      if (this.newTodoTitle.trim()) {
        this.todos.push({ title: this.newTodoTitle, completed: false });
        this.newTodoTitle = '';
      }
    },
    removeTodo(index) {
      this.todos.splice(index, 1);
    },
    updateTodoCompleted(index, completed) {
      this.todos[index].completed = completed;
    },
  },
};
</script>

<style scoped>
.todo-list {
  width: 300px;
  margin: 0 auto;
  padding: 20px;
  border: 1px solid #ccc;
  border-radius: 5px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

.add-todo {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 15px; /* 调整底部间距 */
}
.todo-container {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
  margin-left: 0; /* 确保与输入框左侧对齐 */
  padding-left: 0; /* 确保无额外内边距 */
}
</style>